﻿namespace MDXStudio.QueryEditor
{
    using System;
    using System.Drawing;

    public static class FontUtils
    {
        private static readonly FontStyle[] SUBSTITUTION_SEQUENCE;

        static FontUtils()
        {
            FontStyle[] styleArray = new FontStyle[0x10];
            styleArray[1] = FontStyle.Strikeout;
            styleArray[2] = FontStyle.Underline;
            styleArray[3] = FontStyle.Italic;
            styleArray[4] = FontStyle.Bold;
            styleArray[5] = FontStyle.Strikeout | FontStyle.Underline;
            styleArray[6] = FontStyle.Strikeout | FontStyle.Italic;
            styleArray[7] = FontStyle.Strikeout | FontStyle.Bold;
            styleArray[8] = FontStyle.Underline | FontStyle.Italic;
            styleArray[9] = FontStyle.Underline | FontStyle.Bold;
            styleArray[10] = FontStyle.Italic | FontStyle.Bold;
            styleArray[11] = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic;
            styleArray[12] = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Bold;
            styleArray[13] = FontStyle.Strikeout | FontStyle.Italic | FontStyle.Bold;
            styleArray[14] = FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
            styleArray[15] = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
            SUBSTITUTION_SEQUENCE = styleArray;
        }

        public static Font FontCreate(FontFamily aFamily, float aSize)
        {
            return FontCreate(aFamily.Name, aSize, FontStyle.Regular);
        }

        public static Font FontCreate(string aFamilyName, float aSize)
        {
            return FontCreate(aFamilyName, aSize, FontStyle.Regular);
        }

        public static Font FontCreate(FontFamily aFamily, float aSize, FontStyle aStyle)
        {
            return FontCreate(aFamily.Name, aSize, aStyle);
        }

        public static Font FontCreate(string aFamilyName, float aSize, FontStyle aStyle)
        {
            try
            {
                FontFamily family = new FontFamily(aFamilyName);
                for (int i = 0; i <= 15; i++)
                {
                    FontStyle style = aStyle ^ SUBSTITUTION_SEQUENCE[i];
                    if (family.IsStyleAvailable(style))
                    {
                        return new Font(family, aSize, style);
                    }
                }
            }
            catch
            {
            }
            return new Font("Verdana", aSize, aStyle);
        }
    }
}

